# coding=utf-8
#!/usr/bin/python
import time
import RPi.GPIO as GPIO
import atexit

atexit.register(GPIO.cleanup)

def init():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(11, GPIO.OUT)
    GPIO.setup(12, GPIO.OUT)
    GPIO.setup(15, GPIO.OUT)
    GPIO.setup(16, GPIO.OUT)
    GPIO.setup(38, GPIO.OUT)
def t_stop():
    run(0, 0, 0, 0)


def t_up():
    run(0, 1, 0, 1)


def t_down():
    run(1, 0, 1, 0)


def t_left():
    run(1, 0, 0, 1)


def t_right():
    run(0, 1, 1, 0)


def t_open():
    GPIO.setup(22, GPIO.OUT)
    GPIO.output(22, GPIO.LOW)


##
def run(io11, io12, io15, io16):
    print 'the run method is run'
    GPIO.output(11, io11)
    GPIO.output(12, io12)
    GPIO.output(15, io15)
    GPIO.output(16, io16)


def t_close():
    GPIO.setup(22, GPIO.IN)


def t_servod():
    run(0, 0, 0, 0)
    GPIO.setup(38, GPIO.OUT)
    p = GPIO.PWM(38, 50)
    p.start(0)
    for i in range(0, 181, 10):
        p.ChangeDutyCycle(2.5 + 10 * i / 180)
        time.sleep(0.02)
        p.ChangeDutyCycle(0)
        time.sleep(0.2)
    for i in range(181, 0, -10):
        p.ChangeDutyCycle(2.5 + 10 * i / 180)
        time.sleep(0.02)
        p.ChangeDutyCycle(0)
        time.sleep(0.2)
        #	p.ChangeDutyCycle(7.5)
        #	time.sleep(1)
        #	p.ChangeDutyCycle(7.5)
        #	time.sleep(1)
        #	p.ChangeDutyCycle(2.5)
        #	time.sleep(1)
